<template>
	<view>
		
	</view>
</template>

<script>
	export default {
		name:"scanCode",
		data() {
			return {
				
			};
		},
		created() {
			this.userinfo = this.$store.state.userinfo
		},
		methods: {
			parseQueryString(queryString) {  
			  const params = {};  
			  const pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&');  
			  for (let i = 0; i < pairs.length; i++) {  
			    const pair = pairs[i].split('=');  
			    params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');  
			  }  
			  return params;  
			},
			scanCodeTo(scanClinicId) {
				var _this = this
				if (_this.userinfo.clinic_id_list.includes(scanClinicId)) {
					uni.showToast({ title: '诊所已加入', icon:'error' })
				} else {  
					_this.$api.scan_code_clinic({
						clinicId: scanClinicId,
						types: _this.userinfo.types,
						userId: _this.userinfo.userId,
						name: _this.userinfo.name,
						mobile: _this.userinfo.mobile,
					}).then((res) => {
						_this.userinfo.clinic_data_all.push(res.data)
						_this.userinfo.clinic_id_list.push(res.data._id)
						_this.$store.dispatch('set_userinfo', _this.userinfo)
						uni.showToast({ title:'扫码成功', icon:'success' })
					})
				}
			},
			scanClick() {
				var _this = this
				uni.scanCode({
					success: function (res) {
						uni.showLoading()
						setTimeout(function() {
							console.log('扫码后获取到的诊所ID：' + res.result);
							const parsedParams = _this.parseQueryString(res.result.split('?')[1])
							if ( String(_this.userinfo.types) === parsedParams.types1 || String(_this.userinfo.types) === parsedParams.types0 || String(_this.userinfo.types) === parsedParams.types3 ) {
								_this.scanCodeTo(parsedParams.clinicId)
							} else if (String(_this.userinfo.types) === parsedParams.types2){
								_this.scanCodeTo(parsedParams.clinicId)
							} else {
								uni.showToast({ title: '非法扫码', icon: 'error' })
							}
						}, 1000)
					}
				});
			}
		}
	}
</script>

<style>

</style>